package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.kitchen.conmon.Finals;


import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBookProduct;
import org.itboys.mobile.service.mongo.web.cookbook.KitchenCookBookProductService;
import org.itboys.mobile.service.mongo.web.cookbook.KitchenCookBookService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：花果山（农厨）_菜谱_controller
 */
@Controller
@RequestMapping("/kitchen/web/cookbook")
public class KitchenCookBookController extends BaseController{
    @Autowired
    private KitchenCookBookService cookBookService;
    @Autowired
    private KitchenCookBookProductService cookBookProductService;
    /**
     * 菜谱后台主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model){
        return "/kitchen/cookbook/index";
    }

    /**
     * 后台分页查询菜谱
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<KitchenCookBook> result=cookBookService.selectCookBook(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 菜谱详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            Model model){
        KitchenCookBook cookBook = cookBookService.findOne(id);
        model.addAttribute("cookBook", cookBook);
        return "/kitchen/cookbook/input";
    }
    /**
     * 新增或者修改菜谱
     * @param request
     * @param response
     * @param cookBook
     * @param imageFile
     * @param videoFile
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute KitchenCookBook cookBook,
                     @RequestParam(value="imageFile",required=false)MultipartFile imageFile,
                     @RequestParam(value="videoFile",required=false)MultipartFile videoFile
                     ){
                try{
                    cookBookService.saveCookBook(cookBook,imageFile,videoFile);
                    AjaxUtils.renderJsonporJson(response, null,
                            ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
                }catch (Exception e){
                    String msg ="";
                    if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                        msg=e.getMessage();
                    }else{
                        msg= Finals.ERROMSG;
                        logger.error("/kitchen/web/cookbook/save/------"+e.getMessage());
                    }
                    e.printStackTrace();
                    AjaxUtils.renderJsonporJson(response, null,
                            ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
                }
    }
    /**
     * 删除某个菜谱
     * @param request
     * @param response
     * @param cookBookId
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="cookBookId",required=true) Long cookBookId){
            try {
                    cookBookService.delete(cookBookId);
                AjaxUtils.renderJsonporJson(response, null,
                        ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
            }catch (Exception e){
                e.printStackTrace();
                String msg = "";
                if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                    msg = e.getMessage();
                } else {
                    msg = Finals.ERROMSG;
                    logger.error("/kitchen/web/cookbook/delete------" + e.getMessage());
                }
                e.printStackTrace();

                AjaxUtils.renderJsonporJson(response, null,
                        ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
            }
    }
    /**
     * 设置商品是否推荐。type:1首页推荐
     * @param request
     * @param response
     */
    @RequestMapping("/setRecomment")
    public  void setRecomment(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value="id",required=true) Long id,
                              @RequestParam(value="status",required=false) Integer status,
                              @RequestParam(value="type",required=false) Integer type

    ) {
        try {
            cookBookService.setRecomment(id, status,type);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/kitchen/web/cookbook/setRecomment------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    /*********************菜谱-食材部分**/
    /**
     * 菜谱食材主页
     * @param request
     * @param model
     * @param cookBookId
     * @return
     */
    @RequestMapping("/product/index")
    public String product_index(HttpServletRequest request,
                        Model model,
                        @RequestParam(value="cookBookId",required=true)Long cookBookId){

       model.addAttribute("cookBookId", cookBookId);
        return "/kitchen/cookbook/product/index";
    }
    /**
     * 后台分页查询食材
     * @param request
     * @param response
     */
    @RequestMapping("/product/select")
    public void product_select(HttpServletRequest request,
                       HttpServletResponse response,
                        @RequestParam(value="cookBookId",required=true)Long cookBookId){
        try{
            PageResult<KitchenCookBookProduct> result=cookBookProductService.selectCookBookProduct(request,cookBookId);

            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 菜谱详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/product/input")
    public String product_input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="cookBookId",required=false)Long cookBookId,
            Model model){
        KitchenCookBookProduct cookBookProduct = cookBookProductService.findOne(id,cookBookId);
        model.addAttribute("cookBookProduct", cookBookProduct);
        return "/kitchen/cookbook/product/input";
    }

    /**
     * 新增或保持食材
     * @param request
     * @param response
     * @param cookBookProduct
     */
    @RequestMapping("/product/save")
    public void product_save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute KitchenCookBookProduct cookBookProduct){
        try{
            cookBookProductService.saveCookBookProduct(cookBookProduct);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/kitchen/web/cookbook/product/save------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 新增或保持食材
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/product/delete")
    public void product_delete(HttpServletRequest request,
                             HttpServletResponse response,
                               @RequestParam(value="id",required=true)Long id){
        try{
            cookBookProductService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/kitchen/web/cookbook/product/delete------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 菜谱选择器
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/kitchen/cookbook/choose/choose_erro";
        }else if(isSingle==1){
            return "/kitchen/cookbook/choose/choose_single";
        }else if(isSingle==0){
            return "/kitchen/cookbook/choose/choose_muilt";
        }

        return "/finance/product/choose/choose_erro";
    }
}
